//
//  DataCenter.m
//  LCExpress
//
//  Created by mac1 on 15/10/13.
//  Copyright © 2015年 D0. All rights reserved.
//

#import "DataCenter.h"

#import "HttpManager.h"


@interface DataCenter ()

@property (nonatomic, strong) HttpManager * httpManager ;

@end

@implementation DataCenter

-(DataCenter*)initData
{
    self = [super self];
    
    if(self)
    {
        self.httpManager = [[HttpManager alloc]init];
        
        [self instanceForLogin];
        
        [self instanceForOrder];
        
    }
    return self;
}


-(void)instanceForOrder
{
    //初始化定单接口数据
    self.modOrderRoot = [[ModOrderRoot alloc]init];
    
    self.modOrderDetail = [[ModOrderDetail alloc]init];
    
    self.modODFrom = [[ModODFrom alloc]init];
    
    self.modODFLocation = [[ModODFLocation alloc]init];
    
    self.modODGoods = [[ModODGoods alloc]init];
    
    self.modODTo = [[ModODTo alloc]init];
    
    self.modODTLocation = [[ModODTLocation alloc]init];
    
    self.arrModOD = [[NSMutableArray alloc]init];
}

-(void)instanceForLogin
{
    //初始化登陆接口数据
    self.modLoginRoot = [[ModLoginRoot alloc]init];
    
    self.modLoginInfo = [[ModLoginInfo alloc]init];
    
    self.modEmployee = [[ModEmployee alloc]init];

}

//创建数据中心的单例对象，将其返回给调用者，创建对象时对其初始化，初始化时为数据中心中的数据申请内存空间。
+(DataCenter*)Create
{
    
    static DataCenter * newDataCenter = nil;
    
    if(newDataCenter == nil)
    {

        newDataCenter = [[DataCenter alloc]init];

    }

    return newDataCenter;
}


#pragma mark 获得定单详情
-( void )getOrderDetail
{
    
    NSUserDefaults * userDefault = [ NSUserDefaults standardUserDefaults ];
    
    NSString * token = [userDefault objectForKey:@"token"];
    
    NSString * userid = [userDefault objectForKey:@"userid"];

    //调用HttpManager的方法get数据
    [self.httpManager getWithUrl:GET_ORDER_LIST(token, userid, @"1")  withDataBlock:^(id data) {
        
        NSDictionary  *dataDic = [NSJSONSerialization JSONObjectWithData : data options : NSJSONReadingMutableContainers error : nil ];

        self.modOrderRoot = [ModOrderRoot objectWithKeyValues:dataDic];
        
        //self.modOrderRoot = [ModOrderRoot objectWithKeyValues:dataDic];
        
        self.arrModOD  = [ModOrderDetail objectArrayWithKeyValuesArray:self.modOrderRoot.items];
        
        self.modOrderDetail  = [self.arrModOD objectAtIndex:0];
        
        self.modODFrom = [ModODFrom objectWithKeyValues:self.modOrderDetail.from];
        
        self.modODFLocation = [ModODFLocation objectWithKeyValues:self.modODFrom.location];
        
        self.modODGoods = [ModODGoods objectWithKeyValues:self.modOrderDetail.goods];
        
        self.modODTo = [ModODTo objectWithKeyValues:self.modOrderDetail.to];
        
        self.modODTLocation = [ModODTLocation objectWithKeyValues:self.modODTo.location];
        
        //通知数据使用类，数据准备OK
        if(self.dataIsReady)
            
            self.dataIsReady();
        
    }];

}


#pragma mark 登陆请求
//请求成功后数据序列化，返回码为1登陆成功,抛block到外部模块跳转下一界面，其它返回码抛出block到外部模块弹出alert
-(void)loginWithAccount:(NSString*)account withPassword:(NSString*)password
{
    NSDictionary * dic = [[NSDictionary alloc]init];
    
    [dic setValue:account forKey:@"account"];
    
    [dic setValue:password forKey:@"password"];
    
    [self.httpManager postWithUrl:POST_LOGON withParameters:dic withDataBlock:^(id data) {
        
        NSDictionary  *dataDic = [NSJSONSerialization JSONObjectWithData : data
                                                                                                            options : NSJSONReadingMutableContainers error : nil ];
        
        self.modLoginRoot = [ModLoginRoot objectWithKeyValues:dataDic];
        
        self.modLoginInfo = [ModLoginInfo objectWithKeyValues:self.modLoginRoot.logininfo];
        
        self.modEmployee = [ModEmployee objectWithKeyValues:self.modLoginInfo.employee];
        
        //用户信息序列化
        NSUserDefaults *userDefault = [ NSUserDefaults standardUserDefaults ];
        [userDefault setObject : self.modEmployee.authority forKey : @"authority" ];
        [userDefault setObject : self.modEmployee.avatar forKey : @"avatar" ];
        [userDefault setObject : self.modEmployee.company forKey : @"company" ];
        [userDefault setObject : self.modEmployee.delivery forKey : @"delivery" ];
        [userDefault setObject : self.modEmployee.empno forKey : @"empno" ];
        [userDefault setObject : self.modEmployee.isverified forKey : @"isverified" ];
        [userDefault setObject : self.modEmployee.name forKey : @"name" ];
        [userDefault setObject : self.modEmployee.outlet forKey : @"outlet" ];
        [userDefault setObject : self.modEmployee.phone forKey : @"phone" ];
        [userDefault setObject : self.modEmployee.pickup forKey : @"pickup" ];
        [userDefault setObject : self.modEmployee.range forKey : @"range" ];
        [userDefault setObject : self.modEmployee.rating forKey : @"rating" ];
        [userDefault setObject : self.modEmployee.score forKey : @"score" ];
        [userDefault setObject : self.modEmployee.servicetime forKey : @"servicetime" ];
        [userDefault setObject : self.modEmployee.token forKey : @"token" ];
        [userDefault setObject : self.modEmployee.userid forKey : @"userid" ];
        
        // 将数据从缓冲区写入文件
        [userDefault synchronize];
        
        if([self.modLoginRoot.status isEqual:@"1"])
        {
            
            if(self.loginSuccess)
                
                self.loginSuccess();
            
        }
        else
        {
            
            if(self.loginError)
                
                self.loginError(self.modLoginRoot.msg);
            
        }
    }];
    
}

#pragma mark 获取短信验证码
-(void)getSMSCodeWithPhoneNumber:(NSString*)phoneNumber
{
    
    [self.httpManager  getWithUrl:GET_SMS_VERIFICATION(phoneNumber, @2) withDataBlock:^(id data) {
        
        NSDictionary  *dataDic = [NSJSONSerialization JSONObjectWithData :data options : NSJSONReadingMutableContainers error : nil ];
        
        ModSMSVerifi * modSMSVerifi  = [ModSMSVerifi objectWithKeyValues:dataDic];
        
        NSLog ( @" 返回数据 : %@" ,modSMSVerifi.msg);
        
    }];
    
}

#pragma mark 验证短信验证码，成功登入下一界面
-(void)verifySMSCodeWithPhoneNumber:(NSString*)phoneNumber withSMSNumber:(NSString*)SMSNumber
{
    
    NSDictionary * dic = [[NSDictionary alloc]init];
    
    [dic setValue:phoneNumber forKey:@"phone"];
    
    [dic setValue:@"12341234" forKey:@"password"];
    
    [dic setValue:SMSNumber forKey:@"authcode"];
    
    [dic setValue:@"12341234" forKey:@"deviceid"];
    
    [self.httpManager postWithUrl:POST_SMS_VERIFICATION withParameters:dic withDataBlock:^(id data) {
        
        NSDictionary *dicData = [NSJSONSerialization JSONObjectWithData :data options :
                             NSJSONReadingMutableContainers error : nil ];
        
        ModSMSVerifi * modSMSVerifi = [ModSMSVerifi objectWithKeyValues:dicData];
        
        //NSLog ( @" status = %@, msg = %@ ", modSMSVerifi.status, modSMSVerifi.msg );
        //判断返回状态值，验证成功时进入下一界面，错误时弹出窗口显示错误内容
        
        if([modSMSVerifi.status  isEqual: @"1"])
        {
            
            if(self.verifySuccess)
                
                self.verifySuccess();
            
        }
        else
        {
            
            if(self.verifyFailure)
                
                self.verifyFailure(modSMSVerifi.msg);
            
        }
    }];

}

#pragma mark 通过token获取定单详情

-(void)loginWithToken:(NSString*)token withUserId:(NSString*)userId withDeviceId:(NSString*)deviceId
{
    NSDictionary* dic = [[NSDictionary alloc]init];
    
    [dic setValue:token forKey:@"token"];
    
    [dic setValue:userId forKey:@"userid"];
    
    [dic setValue:deviceId forKey:@"deviceid"];
    
    [self.httpManager postWithUrl:GET_EMPLOYEE(token, userId, deviceId) withParameters:dic withDataBlock:^(id data) {
        
        NSDictionary *dicData = [NSJSONSerialization JSONObjectWithData :data options :
                                 NSJSONReadingMutableContainers error : nil ];
        
        ModSMSVerifi * modSMSVerifi = [ModSMSVerifi objectWithKeyValues:dicData];
    }];
    
}



-(void)saveDataIntoDB
{
    
    //DBManager* dbManager = [DBManager shareDataBase];
    
    //       [dbManager openDatabase:@"data2.sqlite"];
    //
    //       [dbManager createTable:@"create table if not exist orderdetail(serial integer primary key, company varchar(32), companyLogo varchar(32), fromLocationArea varchar(32), fromLocationCity varchar(32), fromLocationDetail varchar(32), fromLocationLat varchar(32), fromLocationLon varchar(32), fromLocationProvince varchar(32), fromLocationStreet varchar(32), fromName varchar(32), fromPhone varchar(32), goodsCount varchar(32), goodsName varchar(32), goodsPayMethod varchar(32), goodsPrice varchar(32), goodsTags varchar(32), goodsType varchar(32), orderNo varchar(32), remark varchar(32), singlePhoto varchar(32), status varchar(32), toLocationArea varchar(32), toLocationCity varchar(32), toLocationDetail varchar(32), toLocationlat varchar(32), toLocationLon varchar(32), toLocationProvince varchar(32), toLocationStreet varchar(32), toName varchar(32), toPhone varchar(32), updateTime varchar(32)"];
}

@end
